package com.blb.community.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.community.entity.SysDictData;
import com.blb.community.entity.SysPost;
import com.blb.community.entity.SysUserPost;
import com.blb.community.mapper.SysPostMapper;
import com.blb.community.service.ISysPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 岗位信息表 服务实现类
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {

    @Resource
    private SysPostMapper sysPostMapper;

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<SysPost> getAll() {
        return sysPostMapper.getAll();
    }

    /**
     * 查询岗位信息
     * @param post 岗位信息
     * @param nowPage 当前页
     * @param pageSize 页面数据大小
     * @return 数据集合
     */
    @Override
    public List<SysPost> selectPostList(SysPost post,int nowPage,int pageSize) {
        return sysPostMapper.selectPostList(post,(nowPage - 1) * pageSize, pageSize);
    }

    /**
     * 修改岗位
     * @param post 岗位信息
     * @return
     */
    @Override
    public int updatePost(SysPost post) {
        //校验
        SysPost name = sysPostMapper.checkPostName(post.getPostName());
        SysPost code = sysPostMapper.checkPostCode(post.getPostCode());

        //查询分配情况
        List<SysUserPost> userPosts = sysPostMapper.selectUserByPostId(post.getPostId());

        //如果岗位名称有重复但岗位编码不重复
        if (name!=null && code==null && !name.getPostId().equals(post.getPostId())) {
            return 2;
        }
        //如果岗位名称不重复但岗位编码重复
        else if (code!=null && !code.getPostId().equals(post.getPostId()) && name==null) {
            return 3;
        }
        //如果岗位名称和编码都重复
        else if (name!=null && code!=null){
            if (!name.getPostId().equals(post.getPostId()) && code.getPostId().equals(post.getPostId())){
                return 2;
            }else if(name.getPostId().equals(post.getPostId()) && !code.getPostId().equals(post.getPostId())){
                return 3;
            }
            //如果没做任何修改也能提交
            else if (name.getPostId().equals(post.getPostId()) && code.getPostId().equals(post.getPostId())){
                if (userPosts.isEmpty()) {
                    return sysPostMapper.updatePost(post);
                }else {
                    if ("0".equals(post.getStatus())){
                        return sysPostMapper.updatePost(post);
                    }else {
                        //岗位被分配，不能停用
                        return 5;
                    }
                }
            }else {
                return 4;
            }
        }
        //如果都不重复 直接修改
        else {
            return sysPostMapper.updatePost(post);
        }
    }


    /**
     * 添加岗位
     * @param post 岗位信息
     * @return
     */
    @Override
    public int insertPost(SysPost post) {
        SysPost name = sysPostMapper.checkPostName(post.getPostName());
        SysPost code = sysPostMapper.checkPostCode(post.getPostCode());
        //如果岗位名称有重复但岗位编码不重复
        if (name!=null && code==null) {
            return 2;
        }
        //如果岗位名称不重复但岗位编码重复
        else if (code!=null && name==null) {
            return 3;
        }
        //如果岗位名称和编码都重复
        else if (name!=null && code!=null ){
            return 4;
        }
        //如果都不重复 直接添加
        else {
            return sysPostMapper.insertPost(post);
        }
    }

    /**
     * 导入    批量添加
     * @param list
     * @return
     */
    @Override
    public int batchAddPost(List<SysPost> list) {
        return sysPostMapper.batchAddPost(list);
    }

    /**
     * 通过岗位ID删除岗位信息
     * @param postId
     * @return
     */
    @Override
    public int deletePostById(Integer postId) {
        //查询是否分配
        List<SysUserPost> userPosts = sysPostMapper.selectUserByPostId(postId);
        //如果没有被分配
        if (userPosts.isEmpty()) {
            return sysPostMapper.deletePostById(postId);
        } else {
            return 3;
        }
    }


    /**
     * 批量删除
     * @param postIds 需要删除的岗位ID数组
     * @return
     */
    @Override
    public int deletePostByIds(Integer[] postIds) {
        //可以删除的数据
        ArrayList<Integer> list = new ArrayList<>();
        //不能删除的数据
        ArrayList<Integer> list1 = new ArrayList<>();
        for (Integer postId:postIds) {
            //查询是否分配
            List<SysUserPost> userPosts = sysPostMapper.selectUserByPostId(postId);
            //如果被分配
            if (userPosts.isEmpty()){
                //能
                list.add(postId);
            }else {
                //不能删
                list1.add(postId);
            }
        }
        if (list1.isEmpty()) {
            return sysPostMapper.deletePostByIds(list.toArray(new Integer[list.size()]));
        }else {
            return 3;
        }
    }

    /**
     * 获取状态（正常/停用）
     * @param dictId 字典主键
     * @return
     */
    @Override
    public List<SysDictData> getDict(Long dictId) {
        return sysPostMapper.getDict(dictId);
    }


    /**
     * 批量查询岗位id  导出
     * @param postIds
     * @return
     */
    @Override
    public List<SysPost> selectByIds(ArrayList<Integer> postIds) {
        if (postIds != null) {
            postIds=postIds.size()==0?null:postIds;
        }
        return sysPostMapper.selectByIds(postIds);
    }


}
